import 'dart:math';

import 'package:flutter/material.dart';

class TransformBox extends StatefulWidget {
  const TransformBox({super.key});

  @override
  State<TransformBox> createState() => _TransformBoxState();
}

class _TransformBoxState extends State<TransformBox> {
  double _rx = 0.0, _ry = 0.0, _rz = 0.0;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onPanUpdate: (details) {
        _rx += details.delta.dx * 0.01;
        _ry += details.delta.dy * 0.01;
        setState(() {
          _rx %= pi * 2;
          _ry %= pi * 2;
        });
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text('旋转盒子'),
          centerTitle: true,
        ),
        body: Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Transform(
              transform: Matrix4.identity()
                ..setEntry(3, 2, 0.001)
                ..rotateX(_rx)
                ..rotateY(_ry)
                ..rotateZ(_rz),
              alignment: Alignment.center,
              child: Cube(),
            ),
            SizedBox(
              height: 20,
            ),
            Slider(
                value: _rx,
                min: 0,
                max: pi * 2,
                onChanged: (value) => setState(() {
                      _rx = value;
                    })),
            Slider(
                value: _ry,
                min: 0,
                max: pi * 2,
                onChanged: (value) => setState(() {
                      _ry = value;
                    })),
            Slider(
                value: _rz,
                min: 0,
                max: pi * 2,
                onChanged: (value) => setState(() {
                      _rz = value;
                    }))
          ],
        )),
      ),
    );
  }
}

class Cube extends StatelessWidget {
  const Cube({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Transform(
          transform: Matrix4.identity()..translate(0.0, 0.0, -100.0),
          child: Container(
            color: Colors.red,
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ),
        Transform(
          transform: Matrix4.identity()..translate(0.0, 0.0, 100.0),
          child: Container(
            color: Colors.green,
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ),
        Transform(
          transform: Matrix4.identity()
            ..translate(100.0, 0.0, 0.0)
            ..rotateY(-pi / 2),
          alignment: Alignment.center,
          child: Container(
            color: Colors.orange,
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ),
        Transform(
          transform: Matrix4.identity()
            ..translate(-100.0, 0.0, 0.0)
            ..rotateY(-pi / 2),
          alignment: Alignment.center,
          child: Container(
            color: Colors.orange[100],
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ),
        Transform(
          transform: Matrix4.identity()
            ..translate(0.0, 100.0, 0.0)
            ..rotateX(pi / 2),
          alignment: Alignment.center,
          child: Container(
            color: Colors.blue,
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ),
        Transform(
          transform: Matrix4.identity()
            ..translate(0.0, -100.0, 0.0)
            ..rotateX(-pi / 2),
          alignment: Alignment.center,
          child: Container(
            color: Colors.black.withOpacity(0.8),
            child: FlutterLogo(
              size: 200,
            ),
          ),
        ),
      ],
    );
  }

  _buildFace() {}
}
